package com.yunpeng.aidevice.dao.service.impl;

import com.yunpeng.aidevice.common.constant.QueryConstants;
import com.yunpeng.aidevice.common.enums.ResultCodeEnum;
import com.yunpeng.aidevice.dao.mapper.IListMapper;
import com.yunpeng.aidevice.dao.service.IListQueryService;
import com.yunpeng.common.exception.BizException;
import com.yunpeng.common.paging.PagingQueryDO;
import com.yunpeng.common.paging.PagingQueryDTO;
import com.yunpeng.common.util.PagingQueryUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * ListQueryServiceImpl
 *
 * @author : Moore(hemw)
 * @date : 2020-04-11
 */
@Service
public class ListQueryServiceImpl implements IListQueryService {

    @Resource
    private IListMapper listMapper;

    @Override
    /**
     *
     * 用户列表页面数据
     *
     * @param queryDTO          查询数据对象
     * @param queryStatement    查询sql
     * @param args              其他参数
     * @return
     * @throws BizException
     */
    public PagingQueryDTO query(PagingQueryDTO queryDTO, String queryStatement, Object[] args) throws BizException {
        if (queryDTO == null) {
            queryDTO = new PagingQueryDTO();
        }
        PagingQueryDO pagingQueryDO = new PagingQueryDO();
        pagingQueryDO.setCurrentPage(queryDTO.getCurrentPage());
        pagingQueryDO.setPageSize(queryDTO.getPageSize(), QueryConstants.MAX_PAGE_SIZE, QueryConstants.DEFAULT_PAGE_SIZE);
        pagingQueryDO.setOrderBy(PagingQueryUtils.getOrderBy(queryDTO));
        pagingQueryDO.setParameters(PagingQueryUtils.getParameters(queryDTO));
        switch (queryStatement) {
            case QueryConstants.QUERY_PERMISSION_LIST_4_SERVICE_SIDE:
                queryDTO = queryPermissionList4ServiceSide(queryDTO, pagingQueryDO);
                break;
            case QueryConstants.QUERY_ROLE_LIST_4_SERVICE_SIDE:
                queryDTO = queryRoleList4ServiceSide(queryDTO, pagingQueryDO);
                break;
            case QueryConstants.QUERY_ORG_PERMISSION_LIST_4_SERVICE_SIDE:
                queryDTO = queryOrgPermissionList4ServiceSide(queryDTO, pagingQueryDO);
                break;
            case QueryConstants.QUERY_DICTIONARIES_LIST_4_SERVICE_SIDE:
                queryDTO = queryDictionariesList4ServiceSide(queryDTO, pagingQueryDO);
                break;
            case QueryConstants.QUERY_USER_LIST_4_MERCHANT:
                queryDTO = queryUserList4Merchant(queryDTO, pagingQueryDO);
                break;
            case QueryConstants.QUERY_DEVICE_LIST_4_MERCHANT:
                queryDTO = queryDeviceList4Merchant(queryDTO, pagingQueryDO);
                break;
            case QueryConstants.QUERY_DEVICE_DAY_DETAIL_RECORD: {
                queryDTO = queryDeviceDayDetailRecord(queryDTO, pagingQueryDO);
                break;
            }
            case QueryConstants.QUERY_DEVICE_DAY_COUNT: {
                String state = (String) args[0];
                queryDTO = queryDeviceDayCount(queryDTO, pagingQueryDO, state);
                break;
            }
            case QueryConstants.QUERY_ORG_DEVICE_DAY_COUNT: {
                String state = (String) args[0];
                queryDTO = queryOrgDeviceDayCount(queryDTO, pagingQueryDO, state);
                break;
            }
            case QueryConstants.QUERY_USER_DEVICE_DAY_COUNT_VIEW: {
                queryDTO = queryUserDeviceRecordCountView(queryDTO, pagingQueryDO);
                break;
            }
            case QueryConstants.QUERY_ORG_DEVICE_DAY_COUNT_VIEW: {
                queryDTO = queryOrgDeviceDayCountView(queryDTO, pagingQueryDO);
                break;
            }
            case QueryConstants.QUERY_MERCHANT_LIST: {
                queryDTO = queryMerchantList(queryDTO, pagingQueryDO);
                break;
            }
            case QueryConstants.QUERY_MERCHANT_DEVICE_DAY_COUNT: {
                String state = (String) args[0];
                queryDTO = queryMerchantDeviceDayCount(queryDTO, pagingQueryDO, state);
                break;
            }
            case QueryConstants.QUERY_MERCHANT_DEVICE_DAYS_RECORD_COUNT: {
                String state = (String) args[0];
                queryDTO = queryMerchantDeviceDaysRecordCount(queryDTO, pagingQueryDO, state);
                break;
            }
            case QueryConstants.QUERY_MERCHANT_RECORD_COUNT_VIEW: {
                queryDTO = queryMerchantDeviceDaysRecordCount(queryDTO, pagingQueryDO);
                break;
            }
            default:
                throw new BizException(ResultCodeEnum.NO_SUCH_METHOD_FAIL, "查询的列表未配置！");
        }
        queryDTO.setSortColumnMap(new LinkedHashMap<>(16));
        queryDTO.setConditionList(new ArrayList<>());
        return queryDTO;
    }


    //region 私有方法


    /**
     * 服务器端权限列表
     *
     * @param queryDTO
     * @param pagingQueryDO
     * @return
     */
    private PagingQueryDTO queryPermissionList4ServiceSide(PagingQueryDTO queryDTO, PagingQueryDO pagingQueryDO) {
        Integer count;
        count = listMapper.countPermissionList4ServiceSide(pagingQueryDO);
        queryDTO.setDataCount(count);
        if (count > 0) {
            List<Map<String, Object>> queryResult = listMapper.queryPermissionList4ServiceSide(pagingQueryDO);
            queryDTO.setData(queryResult);
        }
        return queryDTO;
    }

    /**
     * 服务端角色列表
     *
     * @param queryDTO
     * @param pagingQueryDO
     * @return
     */
    private PagingQueryDTO queryRoleList4ServiceSide(PagingQueryDTO queryDTO, PagingQueryDO pagingQueryDO) {
        Integer count;
        count = listMapper.countRoleList4ServiceSide(pagingQueryDO);
        queryDTO.setDataCount(count);
        if (count > 0) {
            List<Map<String, Object>> queryResult = listMapper.queryRoleList4ServiceSide(pagingQueryDO);
            queryDTO.setData(queryResult);
        }
        return queryDTO;
    }

    /**
     * 服务端组织机构权限列表
     *
     * @param queryDTO
     * @param pagingQueryDO
     * @return
     */
    private PagingQueryDTO queryOrgPermissionList4ServiceSide(PagingQueryDTO queryDTO, PagingQueryDO pagingQueryDO) {
        Integer count;
        count = listMapper.countOrgPermissionList4ServiceSide(pagingQueryDO);
        queryDTO.setDataCount(count);
        queryDTO.setData(new ArrayList<>());
        if (count > 0) {
            List<Map<String, Object>> queryResult = listMapper.queryOrgPermissionList4ServiceSide(pagingQueryDO);
            queryDTO.setData(queryResult);
        }
        return queryDTO;
    }


    /**
     * 服务端字典列表
     *
     * @param queryDTO
     * @param pagingQueryDO
     * @return
     */
    private PagingQueryDTO queryDictionariesList4ServiceSide(PagingQueryDTO queryDTO, PagingQueryDO pagingQueryDO) {
        Integer count;
        count = listMapper.countDictionariesList4ServiceSide(pagingQueryDO);
        queryDTO.setDataCount(count);
        queryDTO.setData(new ArrayList<>());
        if (count > 0) {
            List<Map<String, Object>> queryResult = listMapper.queryDictionariesList4ServiceSide(pagingQueryDO);
            queryDTO.setData(queryResult);
        }
        return queryDTO;
    }

    /**
     * 用户 商户端列表数据
     *
     * @param queryDTO
     * @param pagingQueryDO
     * @return
     */
    private PagingQueryDTO queryUserList4Merchant(PagingQueryDTO queryDTO, PagingQueryDO pagingQueryDO) {
        Integer count;
        count = listMapper.countUserList4Merchant(pagingQueryDO);
        queryDTO.setDataCount(count);
        queryDTO.setData(new ArrayList<>());
        if (count > 0) {
            List<Map<String, Object>> queryResult = listMapper.queryUserList4Merchant(pagingQueryDO);
            queryDTO.setData(queryResult);
        }
        return queryDTO;
    }

    /**
     * 设备 商户端列表数据
     *
     * @param queryDTO
     * @param pagingQueryDO
     * @return
     */
    private PagingQueryDTO queryDeviceList4Merchant(PagingQueryDTO queryDTO, PagingQueryDO pagingQueryDO) {
        Integer count;
        count = listMapper.countDeviceList4Merchant(pagingQueryDO);
        queryDTO.setDataCount(count);
        queryDTO.setData(new ArrayList<>());
        if (count > 0) {
            List<Map<String, Object>> queryResult = listMapper.queryDeviceList4Merchant(pagingQueryDO);
            queryDTO.setData(queryResult);
        }
        return queryDTO;
    }


    /**
     * 设备 单日详情数据
     *
     * @param queryDTO
     * @param pagingQueryDO
     * @return
     */
    private PagingQueryDTO queryDeviceDayDetailRecord(PagingQueryDTO queryDTO, PagingQueryDO pagingQueryDO) {
        Integer count;
        count = listMapper.countDeviceDayDetailRecord(pagingQueryDO);
        queryDTO.setDataCount(count);
        queryDTO.setData(new ArrayList<>());
        if (count > 0) {
            List<Map<String, Object>> queryResult = listMapper.queryDeviceDayDetailRecord(pagingQueryDO);
            queryDTO.setData(queryResult);
        }
        return queryDTO;
    }

    /**
     * 设备 日统计数据
     *
     * @param queryDTO
     * @param pagingQueryDO
     * @param state
     * @return
     */
    private PagingQueryDTO queryDeviceDayCount(PagingQueryDTO queryDTO, PagingQueryDO pagingQueryDO, String state) {
        Integer count;
        count = listMapper.countDeviceDayCount(pagingQueryDO, state);
        queryDTO.setDataCount(count);
        queryDTO.setData(new ArrayList<>());
        if (count > 0) {
            List<Map<String, Object>> queryResult = listMapper.queryDeviceDayCount(pagingQueryDO, state);
            queryDTO.setData(queryResult);
        }
        return queryDTO;
    }

    /**
     * 组织 设备统计数据
     *
     * @param queryDTO
     * @param pagingQueryDO
     * @param state
     * @return
     */
    private PagingQueryDTO queryOrgDeviceDayCount(PagingQueryDTO queryDTO, PagingQueryDO pagingQueryDO, String state) {
        Integer count;
        count = listMapper.countOrgDeviceDayCount(pagingQueryDO, state);
        queryDTO.setDataCount(count);
        queryDTO.setData(new ArrayList<>());
        if (count > 0) {
            List<Map<String, Object>> queryResult = listMapper.queryOrgDeviceDayCount(pagingQueryDO, state);
            queryDTO.setData(queryResult);
        }
        return queryDTO;
    }

    /**
     * 用户 设备统计数据
     *
     * @param queryDTO
     * @param pagingQueryDO
     * @return
     */
    private PagingQueryDTO queryUserDeviceRecordCountView(PagingQueryDTO queryDTO, PagingQueryDO pagingQueryDO) {
        Integer count;
        count = listMapper.countUserDeviceRecordCountView(pagingQueryDO);
        queryDTO.setDataCount(count);
        queryDTO.setData(new ArrayList<>());
        if (count > 0) {
            List<Map<String, Object>> queryResult = listMapper.queryUserDeviceRecordCountView(pagingQueryDO);
            queryDTO.setData(queryResult);
        }
        return queryDTO;
    }

    /**
     * 组织 设备统计数据
     *
     * @param queryDTO
     * @param pagingQueryDO
     * @return
     */
    private PagingQueryDTO queryOrgDeviceDayCountView(PagingQueryDTO queryDTO, PagingQueryDO pagingQueryDO) {
        Integer count;
        count = listMapper.countOrgDeviceRecordCountView(pagingQueryDO);
        queryDTO.setDataCount(count);
        queryDTO.setData(new ArrayList<>());
        if (count > 0) {
            List<Map<String, Object>> queryResult = listMapper.queryOrgDeviceDayCountView(pagingQueryDO);
            queryDTO.setData(queryResult);
        }
        return queryDTO;
    }

    /**
     * 商户列表数据
     *
     * @param queryDTO
     * @param pagingQueryDO
     * @return
     */
    private PagingQueryDTO queryMerchantList(PagingQueryDTO queryDTO, PagingQueryDO pagingQueryDO) {
        Integer count;
        count = listMapper.countMerchantList(pagingQueryDO);
        queryDTO.setDataCount(count);
        queryDTO.setData(new ArrayList<>());
        if (count > 0) {
            List<Map<String, Object>> queryResult = listMapper.queryMerchantList(pagingQueryDO);
            queryDTO.setData(queryResult);
        }
        return queryDTO;
    }

    /**
     * 商户单日设备
     *
     * @param queryDTO
     * @param pagingQueryDO
     * @param state
     * @return
     */
    private PagingQueryDTO queryMerchantDeviceDayCount(PagingQueryDTO queryDTO, PagingQueryDO pagingQueryDO, String state) {
        Integer count;
        count = listMapper.countMerchantDeviceDayCount(pagingQueryDO, state);
        queryDTO.setDataCount(count);
        queryDTO.setData(new ArrayList<>());
        if (count > 0) {
            List<Map<String, Object>> queryResult = listMapper.queryMerchantDeviceDayCount(pagingQueryDO, state);
            queryDTO.setData(queryResult);
        }
        return queryDTO;
    }

    /**
     * 商户设备汇总
     *
     * @param queryDTO
     * @param pagingQueryDO
     * @param state
     * @return
     */
    private PagingQueryDTO queryMerchantDeviceDaysRecordCount(PagingQueryDTO queryDTO, PagingQueryDO pagingQueryDO, String state) {
        Integer count;
        count = listMapper.countMerchantDeviceDaysRecordCount(pagingQueryDO, state);
        queryDTO.setDataCount(count);
        queryDTO.setData(new ArrayList<>());
        if (count > 0) {
            List<Map<String, Object>> queryResult = listMapper.queryMerchantDeviceDaysRecordCount(pagingQueryDO, state);
            queryDTO.setData(queryResult);
        }
        return queryDTO;
    }


    /**
     * 商户体温走势
     *
     * @param queryDTO
     * @param pagingQueryDO
     * @return
     */
    private PagingQueryDTO queryMerchantDeviceDaysRecordCount(PagingQueryDTO queryDTO, PagingQueryDO pagingQueryDO) {
        Integer count;
        count = listMapper.countMerchantRecordCountView(pagingQueryDO);
        queryDTO.setDataCount(count);
        queryDTO.setData(new ArrayList<>());
        if (count > 0) {
            List<Map<String, Object>> queryResult = listMapper.queryMerchantRecordCountView(pagingQueryDO);
            queryDTO.setData(queryResult);
        }
        return queryDTO;
    }
    //endregion
}
